package com.ftar.urgent.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Looper;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;

import com.ftar.urgent.http.model.PictureJsonBean;

public class OverlayView extends View {
    private String TAG = this.getClass().getSimpleName();
    private PictureJsonBean mThingsList;
    private TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    private Paint rectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint rectFillPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private int fontSize = 40;

    public OverlayView(Context context) {
        super(context);

        init();
    }

    public OverlayView(Context context, AttributeSet attrs) {
        super(context, attrs);

        init();
    }

    private void init() {
        initPaint();
        observeViewSize();
    }

    private void initPaint() {
        rectPaint.setTextSize(fontSize);
        rectPaint.setStyle(Paint.Style.STROKE);
        rectPaint.setColor(Color.GREEN);
        rectPaint.setAntiAlias(false);
        rectPaint.setStrokeWidth(5);

        rectFillPaint.setStyle(Paint.Style.FILL);
        rectFillPaint.setColor(Color.GREEN);
        rectFillPaint.setAntiAlias(false);
        rectFillPaint.setTextSize(fontSize);
        rectFillPaint.setStrokeWidth(2);

        textPaint.setTextSize(fontSize);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setColor(Color.WHITE);
        textPaint.setAntiAlias(false);
        textPaint.setStrokeWidth(2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mThingsList == null) {
            return;
        }

        drawRect(mThingsList, canvas);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

//        int height = widthMeasureSpec * CameraConfig.CAMERA_PREVIEW_HEIGHT / CameraConfig.CAMERA_PREVIEW_WIDTH;
//        setMeasuredDimension(widthMeasureSpec, height);
    }

    /**
     * 监听View大小，
     * Notice: ratioX,ratioY,为预览图片和蒙版画布大小的比例，FaceSDK从图片获取的像素需要MAP到蒙版对应的位置
     */
    private void observeViewSize() {
        ViewTreeObserver vto = getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

            }
        });
    }

    public void setListInfo(PictureJsonBean infoList) {
        mThingsList = infoList;
        invalidateView();
    }

    /**
     * 绘制识别框
     * @param canvas 当前View画布
     */
    private void drawRect(PictureJsonBean thingsModel, Canvas canvas) {
        if(thingsModel == null) {
            return;
        }

        Rect rect = new Rect(
                (int)thingsModel.getX1(),
                (int)thingsModel.getY1(),
                (int)thingsModel.getX2(),
                (int)thingsModel.getY2());

        canvas.drawRect(rect, rectPaint);
        drawOutLabel(canvas, "", thingsModel.getType(), rect, 10, rectFillPaint);
    }

    private void drawOutLabel(Canvas canvas, String header, String content, Rect rect, int offset, Paint paint) {
        canvas.drawText(header + content, rect.left, rect.top - offset, paint);
    }

    /**
     * clear view
     */
    public void clear() {
        mThingsList = null;
        invalidateView();
    }

    private void invalidateView() {
        if (Looper.getMainLooper().getThread() == Thread.currentThread()) {
            invalidate();
        } else {
            postInvalidate();
        }
    }

}